﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="lisadokumendid_paring", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(LisadokumendidParing))]
    public class LisadokumendidParingSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected ILisadokumendidParing clazz;
        
        public LisadokumendidParingSerializer(ILisadokumendidParing clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("ettevotja_id");
            if (clazz.EttevotjaId!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EttevotjaId))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : EttevotjaId");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("kande_id");
            if (clazz.KandeId!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.KandeId))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : KandeId");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("menetlused_id");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "int");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.MenetlusedId))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("ekande_liik");
            Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "int");
            Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.EkandeLiik))).ToString());
            Writer.WriteEndElement();
            Writer.WriteStartElement("esitaja_nimi");
            if (clazz.EsitajaNimi!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EsitajaNimi))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : EsitajaNimi");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("esitaja_kood");
            if (clazz.EsitajaKood!=null) {
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.EsitajaKood))).ToString());
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : EsitajaKood");
            }
            Writer.WriteEndElement();
            if (clazz.Markus!=null) {
                Writer.WriteStartElement("markus");
                Writer.WriteTypeAttribute("http://www.w3.org/2001/XMLSchema", "string");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Markus))).ToString());
                Writer.WriteEndElement();
            }
            if (((clazz.Lisadokumendid == null) 
                        || (clazz.Lisadokumendid.Count < 1m))) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "lisadokumendid");
            }
            for (int x = 0; ((clazz.Lisadokumendid != null) 
                        && (x < clazz.Lisadokumendid.Count)); x = (x + 1)) {
                Writer.WriteStartElement("lisadokumendid");
                if (clazz.Lisadokumendid[x]!=null) {
                    global::Xtee.Core.SerializationMetadata metaLisadokumendid = this.GetMetaData(clazz.Lisadokumendid[x]);
                    Writer.WriteTypeAttribute(metaLisadokumendid.TypeQualifiedName.Namespace, metaLisadokumendid.TypeQualifiedName.Name);
                    metaLisadokumendid.NewSerializer(clazz.Lisadokumendid[x]).WriteXml(Writer);
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Lisadokumendid");
                }
                Writer.WriteEndElement();
            }
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="ettevotja_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>ettevotja_id");
                }
                clazz.EttevotjaId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("ettevotja_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>ettevotja_id", Reader.LocalName);
            }
            if (Reader.LocalName=="kande_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>kande_id");
                }
                clazz.KandeId = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("kande_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>kande_id", Reader.LocalName);
            }
            if (Reader.LocalName=="menetlused_id") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>menetlused_id");
                }
                clazz.MenetlusedId = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("menetlused_id")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>menetlused_id", Reader.LocalName);
            }
            if (Reader.LocalName=="ekande_liik") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>ekande_liik");
                }
                clazz.EkandeLiik = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("ekande_liik")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>ekande_liik", Reader.LocalName);
            }
            if (Reader.LocalName=="esitaja_nimi") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>esitaja_nimi");
                }
                clazz.EsitajaNimi = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("esitaja_nimi")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>esitaja_nimi", Reader.LocalName);
            }
            if (Reader.LocalName=="esitaja_kood") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>esitaja_kood");
                }
                clazz.EsitajaKood = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("esitaja_kood")).Value;
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>esitaja_kood", Reader.LocalName);
            }
            if (Reader.LocalName=="markus") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>markus");
                }
                clazz.Markus = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("markus")).Value;
            }
            clazz.Lisadokumendid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ILisadokumentType>();
            for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                        && (Reader.LocalName == "lisadokumendid")); ) {
                if (Reader.LocalName=="lisadokumendid") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>lisadokumendid_paring>lisadokumendid");
                    }
                    global::Xtee.Core.SerializationMetadata metaLisadokumendid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ILisadokumentType>(Reader, "lisadokument_type", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                    global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ILisadokumentType item = metaLisadokumendid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.ILisadokumentType>();
                    clazz.Lisadokumendid.Add(item);
                    if ((Reader.IsEmptyElement == false)) {
                        Reader.ReadStartElement("lisadokumendid");
                        metaLisadokumendid.NewSerializer(item).ReadXml(Reader);
                        Reader.ReadEndElement();
                    }
                    else {
                        Reader.Skip();
                    }
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>lisadokumendid_paring>lisadokumendid", Reader.LocalName);
                }
            }
            if ((clazz.Lisadokumendid.Count < 1m)) {
                throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "lisadokumendid");
            }
        }
    }
}
